package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.*;
import cn.edu.sdjzu.xg.bysj.domain.*;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public final class StudentClassService {
    private static StudentClassDao studentClassDao= StudentClassDao.getInstance();
    private static StudentClassService studentClassService=null;
    private StudentClassService(){}

    public static StudentClassService getInstance(){
        if (studentClassService == null){
            studentClassService = new StudentClassService();
        }
        return studentClassService;
    }

    /**
     * 根据条件查找StudentClass对象
     *
     * @author gty2001128@163.com
     * @param pagination 分页信息
     * @param conditionList_json_str 查询条件
     * @return 查询结果
     * @throws SQLException
     */
    public Collection<StudentClass> findAll
            (Pagination pagination,List<Condition> conditionList_json_str)
            throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<StudentClass> studentClasses = new ArrayList<StudentClass>();
        studentClasses = studentClassDao.findAll(pagination,conditionList_json_str,connection);
        JdbcHelper.close(connection);
        JdbcHelper.close(connection);
        return studentClasses;
    }

    /**
     * 根据id查找StudentClass对象
     *
     * @author gty2001128@163.com
     * @param id 要查询的StudentClass对象的id
     * @return 查询结果
     * @throws SQLException
     */
    public StudentClass find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        StudentClass studentClass = studentClassDao.find(id,connection);
        JdbcHelper.close(connection);
        JdbcHelper.close(connection);
        return studentClass;
    }

    /**
     * 添加StudentClass对象
     *
     * @author gty2001128@163.com
     * @param studentClass 要添加的StudentClass对象
     * @return 添加成功返回true，否则false
     * @throws SQLException
     */
    public boolean add(StudentClass studentClass) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            //增加用户
            added = studentClassDao.add(studentClass, connection);
            //提交事务
            connection.commit();

        }catch (SQLException e){
            //回滚
            connection.rollback();
            throw new BysjException("新添加的班级的编号已经存在");
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added;
    }


    /**
     * 根据id删除StudentClass对象
     *
     * @author gty2001128@163.com
     * @param id 要删除的StudentClass对象
     * @return 删除成功返回true，否则false
     * @throws SQLException
     */
    public boolean delete(Integer id) throws SQLException,BysjException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean updated = false;
        try{
            //获得所有下一级单位（DepartmentController）
            int countTeacherOfStudentClass = CommonDao.getInstance().
                    countWhereForeignKey(connection,"student","studentClass_id",id);
            if(countTeacherOfStudentClass>0) {
                //添加teacher对象，返回值为自动生成的id
                studentClassDao.delete(id, connection);
                //提交事务
                connection.commit();
            }
        }catch (SQLException e){
            //回滚
            connection.rollback();
            throw new BysjException("删除失败");
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    /**
     * 根据对象修改StudentClass对象
     *
     * @author gty2001128@163.com
     * @param studentClass 要修改的StudentClass对象
     * @return 修改成功返回true，否则false
     * @throws SQLException
     */
    public boolean update(StudentClass studentClass) throws SQLException,BysjException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean updated = false;
        try{
            studentClassDao.update(studentClass,connection);
            //提交事务
            connection.commit();

        }catch (SQLException e){
            //回滚
            connection.rollback();
            throw new BysjException("修改失败");
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }
}